package br.com.educa.modelo.academico;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

import br.com.educa.modelo.academico.enumerator.SituacaoAluno;
import br.com.educa.modelo.financeiro.LancamentosAlunos;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Table
@Configurable
@RooJson
@RooSerializable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findAlunosTurmasesByTurma", "findAlunosTurmasesByAnoEquals", "findAlunosTurmasesByAluno" })
public class AlunosTurmas implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    private Integer numeroChamanda;

    private Integer ano;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataMatricula;

    @Enumerated(EnumType.STRING)
    private SituacaoAluno situacao;

    private Boolean recuperacao;

    private Integer percentualPresenca;

    @ManyToOne
    @JoinColumn(name = "aluno", referencedColumnName = "id", nullable = false)
    private Alunos aluno;

    @ManyToOne
    @JoinColumn(name = "turma", referencedColumnName = "id", nullable = false)
    private Turmas turma;

    @PersistenceContext
    transient EntityManager entityManager;

    private static final long serialVersionUID = 1L;

    public Turmas getTurma() {
        return turma;
    }

    public void setTurma(Turmas turma) {
        this.turma = turma;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.academico.AlunosTurmas fromJsonToAlunosTurmas(String json) {
        return new JSONDeserializer<AlunosTurmas>().use(null, AlunosTurmas.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.academico.AlunosTurmas> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.academico.AlunosTurmas> fromJsonArrayToAlunosTurmases(String json) {
        return new JSONDeserializer<List<AlunosTurmas>>().use(null, ArrayList.class).use("values", AlunosTurmas.class).deserialize(json);
    }

    public static final EntityManager entityManager() {
        EntityManager em = new AlunosTurmas().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countAlunosTurmases() {
        return entityManager().createQuery("SELECT COUNT(o) FROM AlunosTurmas o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.academico.AlunosTurmas> findAllAlunosTurmases() {
        return entityManager().createQuery("SELECT o FROM AlunosTurmas o", AlunosTurmas.class).getResultList();
    }

    public static br.com.educa.modelo.academico.AlunosTurmas findAlunosTurmas(Long id) {
        if (id == null) return null;
        return entityManager().find(AlunosTurmas.class, id);
    }

    public static List<br.com.educa.modelo.academico.AlunosTurmas> findAlunosTurmasEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM AlunosTurmas o", AlunosTurmas.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            AlunosTurmas attached = AlunosTurmas.findAlunosTurmas(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.academico.AlunosTurmas merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        AlunosTurmas merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getNumeroChamanda() {
        return this.numeroChamanda;
    }

    public void setNumeroChamanda(Integer numeroChamanda) {
        this.numeroChamanda = numeroChamanda;
    }

    public Integer getAno() {
        return this.ano;
    }

    public void setAno(Integer ano) {
        this.ano = ano;
    }

    public Date getDataMatricula() {
        return this.dataMatricula;
    }

    public void setDataMatricula(Date dataMatricula) {
        this.dataMatricula = dataMatricula;
    }

    public SituacaoAluno getSituacao() {
        return this.situacao;
    }

    public void setSituacao(SituacaoAluno situacao) {
        this.situacao = situacao;
    }

    public Boolean getRecuperacao() {
        return this.recuperacao;
    }

    public void setRecuperacao(Boolean recuperacao) {
        this.recuperacao = recuperacao;
    }

    public Integer getPercentualPresenca() {
        return this.percentualPresenca;
    }

    public void setPercentualPresenca(Integer percentualPresenca) {
        this.percentualPresenca = percentualPresenca;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Alunos getAluno() {
        return this.aluno;
    }

    public void setAluno(Alunos aluno) {
        this.aluno = aluno;
    }

    public static TypedQuery<br.com.educa.modelo.academico.AlunosTurmas> findAlunosTurmasesByTurma(Turmas turma) {
        if (turma == null) throw new IllegalArgumentException("The turma argument is required");
        EntityManager em = AlunosTurmas.entityManager();
        TypedQuery<AlunosTurmas> q = em.createQuery("SELECT o FROM AlunosTurmas AS o WHERE o.turma = :turma", AlunosTurmas.class);
        q.setParameter("turma", turma);
        return q;
    }

    public static TypedQuery<br.com.educa.modelo.academico.AlunosTurmas> findAlunosTurmasesByAnoEquals(Integer ano) {
        if (ano == null) throw new IllegalArgumentException("The ano argument is required");
        EntityManager em = AlunosTurmas.entityManager();
        TypedQuery<AlunosTurmas> q = em.createQuery("SELECT o FROM AlunosTurmas AS o WHERE o.ano = :ano", AlunosTurmas.class);
        q.setParameter("ano", ano);
        return q;
    }

	public static TypedQuery<AlunosTurmas> findAlunosTurmasesByAluno(Alunos aluno) {
        if (aluno == null) throw new IllegalArgumentException("The aluno argument is required");
        EntityManager em = AlunosTurmas.entityManager();
        TypedQuery<AlunosTurmas> q = em.createQuery("SELECT o FROM AlunosTurmas AS o WHERE o.aluno = :aluno", AlunosTurmas.class);
        q.setParameter("aluno", aluno);
        return q;
    }

}
